package cn.org.timespace.ons.dao.policy.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.org.timespace.ons.action.policy.MomStatusModel;
import cn.org.timespace.ons.dao.TSBaseDAO;
import cn.org.timespace.ons.dao.policy.MomStatusDao;
import cn.org.timespace.ons.util.pager.Pager;
import cn.org.timespace.ons.vo.policy.MomStatus;

public class MomStatusDaoImpl  extends TSBaseDAO implements MomStatusDao{
	@Override
	public MomStatus findById(String id) {
			// TODO Auto-generated method stub
			log.debug("getting MomStatus instance with id: " +id);
			try{
				MomStatus instance = (MomStatus)getHibernateTemplate().get(
						"cn.org.timespace.ons.vo.policy.MomStatus", id);
				return instance;
			}catch(RuntimeException re){
				log.debug("get fail",re);
				throw re;
		}
	}
	
	@Override
	public void save(MomStatus instance) {
	
		log.debug("saving Mom Instance");
		try{
			getHibernateTemplate().save(instance);
			log.debug("saving successful");
		}catch(RuntimeException re){
			log.debug("saving fail",re);
			throw re;
		}
	}

	@Override
	public void delete(MomStatus instance) {
		log.debug("deleting Mom Instance");
		try{
			getHibernateTemplate().delete(instance);
			log.debug("delete successful");
		}catch(RuntimeException re){
			log.debug("delete fail",re);
			throw re;
		}
	}
	
	@Override
	public void update(MomStatus instance) {
		
		log.debug("update Mom Instance");
		try{
			getHibernateTemplate().update(instance);
			
		}catch(RuntimeException re){
			log.error("update failed!",re);
			throw re;
		}	
	}

	@Override
	public List<MomStatus> findByCondition(MomStatusModel model) {
		log.debug("findByCondition Mom Instance");
		StringBuffer queryString = new StringBuffer("from MomStatus m where 1=1");
		if(!StringUtils.isEmpty(model.getMomid())){
			queryString.append("and m.momid = '" + model.getMomid() + "'" );
		}
		if(!StringUtils.isEmpty(model.getStatus())){
			queryString.append("and m.status = '" + model.getStatus() + "'" );
		}
		queryString.append(" order by m.sentTime desc");
		return getHibernateTemplate().find(queryString.toString());
	}

	
	
	@Override
	public List<Object> findByCondition(MomStatusModel model, Pager p) {
	    log.debug("finding all findByCondition instances");
		Session session = null;
		Query query = null;
		try {
			StringBuffer queryString = new StringBuffer("from MomStatus m where 1=1");
			if(!StringUtils.isEmpty(model.getMomid())){
				queryString.append("and m.momid = '" + model.getMomid() + "'" );
			}
			queryString.append(" order by m.sentTime desc");
			session = getSessionFactory().openSession();
			query = session.createQuery(queryString.toString());
			List<Object> resList = new ArrayList();
			resList.add(query.list().size());
			setQueryPager(query, p);
			resList.add(query.list());
			return resList;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			closeSessionAndQuery(session, query);
		}
	}
}
